package com.itheima.api.config;

import com.hmall.common.utils.UserContext;
import com.itheima.api.client.fallback.ItemFallback;
import feign.Logger;
import feign.RequestInterceptor;
import feign.RequestTemplate;
import org.springframework.context.annotation.Bean;

import java.time.LocalDateTime;


public class FeignConfig {

    @Bean
    public Logger.Level feignLogLevel(){
        return Logger.Level.FULL;
    }

    @Bean
    public RequestInterceptor userInfoRequestInterceptor(){
        return new RequestInterceptor() {
            @Override
            public void apply(RequestTemplate requestTemplate) {
                //  获取userId
                Long userId = UserContext.getUser();
                System.err.println("<2>微服务互相调用 api 拦截器中 userInfo= "+userId +" --- time: "+ LocalDateTime.now());
                if (userId == null){
                    return;
                }
                requestTemplate.header("user-info",userId.toString());

            }
        };
    }

    @Bean
    public ItemFallback itemFallback(){
        return new ItemFallback();
    }
}
